<?php

namespace plugin\admin\app\controller;

use support\Request;
use support\Response;
use plugin\admin\app\model\RlShortcut;
use support\exception\BusinessException;

/**
 * 快捷功能列表 
 */
class RlShortcutController extends Crud
{
    
    /**
     * @var RlShortcut
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new RlShortcut;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('rl-shortcut/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $shortcutTitle=$request->post('shortcut_title','');
            $shortcutIcon=$request->post('shortcut_icon','');
            $sort=$request->post('sort',0)?:0;
            $shortcutType=$request->post('shortcut_type',0)?:0;
            $shortcutValue=$request->post('shortcut_value',0)?:0;
            $ret=$this->model->insert([
                'shortcut_title'=>$shortcutTitle,
                'shortcut_icon'=>$shortcutIcon,
                'sort'=>$sort,
                'shortcut_type'=>$shortcutType,
                'shortcut_value'=>$shortcutValue,
                'create_time'=>time()
            ]);
            if (!$ret){
                return $this->json(-1);
            }
            return $this->json(0);
        }
        return view('rl-shortcut/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            $request->update_time=time();
            return parent::update($request);
        }
        return view('rl-shortcut/update');
    }

}
